package com.base.themvx.action

import android.view.View
import com.base.themvx.isValidClick

/**
 * <p>
 * </p>
 *
 * @author Kingbo
 * @date 2025/4/19
 */
interface ClickAction : View.OnClickListener {

    companion object {
        private const val FAST_CLICK_GAP_TIME = 400L
    }

    // ----------------------------------------------------- @ 同时设置多个控件的点击事件

    /**
     * 阻止快速点击
     *
     * @param view View
     */
    fun setOnClickListenerWithPreventFast(view: View) {
        view.setOnClickListener { v ->
            if (!v.isValidClick(FAST_CLICK_GAP_TIME)) return@setOnClickListener
            onClickPreventFast(v)
        }
    }

    /**
     * 阻止快速点击
     */
    fun setOnClickListenerWithPreventFast(vararg views: View) {
        views.forEach {
            setOnClickListenerWithPreventFast(it)
        }
    }

    override fun onClick(view: View?) {
        // 默认不实现，让子类实现
    }

    private fun onClickPreventFast(view: View) {
        onClick(view)
    }

}
